package cn.edu.sjtu;

import hu.uw.pallergabor.dedexer.Dedexer;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import cn.edu.sjtu.dfa.Instruction;

public class DataFlowAnalyzer {
	public static HashMap<String, List<Instruction>> ils;
	public static HashMap<Long, Long> filePos2Line;	// TODO
	
	public static boolean isDataFlowAnalysis = false;
	
	private String inPath;
	private String outPath;
	
	public DataFlowAnalyzer(String out, String in) {
		outPath = out;
		inPath = in;
	}
	
	public void Analyse() {
		ils = new HashMap<String, List<Instruction>>();
		filePos2Line = new HashMap<Long, Long>();
		isDataFlowAnalysis = true;
		
		Dedexer dedexer = new Dedexer();
		String[] args = { "-r", "-d", outPath, inPath };
		dedexer.run(args);
		
		isDataFlowAnalysis = false;
	}
	
	public void Print() {
		if (ils != null) {
			Iterator it = ils.entrySet().iterator();
			while (it.hasNext()) {
				Map.Entry entry = (Map.Entry) it.next();
				if (!entry.getKey().toString().equals("cn.edu.sjtu.test.Test"))
					continue;
				System.out.println("Class: " + entry.getKey());
				
				List<Instruction> list = (List<Instruction>) entry.getValue();
				if (list != null) {
					for (Instruction i : list) {
						System.out.println("\tInstruction Length: " + i.getinstrLength());
						System.out.println("\tLine: " + filePos2Line.get(i.getFilePosition()));
						System.out.println("\t" + i.toString());
						System.out.println("\tDex Code: " + i.getDexerString());
						System.out.println("-------------------------------");
					}
				}
			}
		}
	}
	
	public static void main(String[] args) {
		DataFlowAnalyzer dfa = new DataFlowAnalyzer("D:\\Temp\\ContentProviderTest\\dfa", "D:\\Temp\\ContentProviderTest\\classes.dex");
		dfa.Analyse();
		dfa.Print();
	}
}
